﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Xml
Imports System.Web.UI.WebControls

Namespace Control
    <DefaultProperty("Text"), ToolboxData("<{0}:Loader runat=server></{0}:Loader>")> _
    Public Class Loader
        Inherits UI.Control
        '配置文件路径
        Private _configPath As String = "~/CustomControl/ERP.xml"

        Public Property ConfigPath() As String
            Get
                Return _configPath
            End Get
            Set(ByVal value As String)
                _configPath = value
            End Set
        End Property

        '页面引用控件,各控件件通过逗号分割，不区分大小写
        Private _control As String = ""

        Public Property Control() As String
            Get
                Return _control
            End Get
            Set(ByVal value As String)
                _control = value.ToLower().Trim()
            End Set
        End Property

        '页面调用哪些子系统
        Private _system As String = ""

        Public Property System() As String
            Get
                Return _system
            End Get
            Set(ByVal value As String)
                _system = value.ToLower().Trim()
            End Set
        End Property

        '自定义资源
        Private _custom As String = ""

        Public Property Custom() As String
            Get
                Return _custom
            End Get
            Set(ByVal value As String)
                _custom = value.ToLower().Trim()
            End Set
        End Property

        Protected Overrides Sub Render(ByVal output As HtmlTextWriter)
            Dim httpContext As HttpContext = httpContext.Current
            Dim fileList As List(Of String) = New List(Of String)()
            Dim xmldoc As XmlDocument = Nothing
            '将xml放在全局对象中提高性能
            If httpContext.Application("loadConfigXmlDoc") Is Nothing Then
                xmldoc = New XmlDocument()
                xmldoc.Load(httpContext.Server.MapPath(_configPath))
                httpContext.Application("loadConfigXmlDoc") = xmldoc
            Else
                xmldoc = CType(httpContext.Application("loadConfigXmlDoc"), XmlDocument)
            End If
            '遍历添加公共资源

            Dim commonXnlNodes As XmlNodeList = xmldoc.SelectNodes("/erp/resource/common/file")
            AppendFiles(fileList, commonXnlNodes, output)

            '遍历自定义调用资源
            If Not String.IsNullOrEmpty(_custom) Then
                For Each controlName As String In _custom.Split(","c)
                    Dim xnlNodes As XmlNodeList = xmldoc.SelectNodes(String.Format("/erp/resource/custom/{0}/file", controlName.Trim()))
                    AppendFiles(fileList, xnlNodes, output)
                Next controlName
            End If

            '遍历添加控件资源
            If Not String.IsNullOrEmpty(_control) Then
                For Each controlName As String In _control.Split(","c)
                    Dim xnlNodes As XmlNodeList = xmldoc.SelectNodes(String.Format("/erp/resource/controls/{0}/file", controlName.Trim()))
                    AppendFiles(fileList, xnlNodes, output)
                Next controlName
            End If
            

            '遍历添加跨系统调用资源
            If Not String.IsNullOrEmpty(_system) Then
                For Each controlName As String In _system.Split(","c)
                    Dim xnlNodes As XmlNodeList = xmldoc.SelectNodes(String.Format("/erp/resource/system/{0}/file", controlName.Trim()))
                    AppendFiles(fileList, xnlNodes, output)
                Next controlName
            End If
        End Sub

        '在页面输出文件
        Protected Sub AppendFiles(ByRef fileList As List(Of String), ByVal xnlNodes As XmlNodeList, ByVal output As HtmlTextWriter)
            Dim filePath As String = ""
            Dim fileType As String = ""
            For Each node As XmlNode In xnlNodes
                filePath = node.Attributes("path").Value.ToLower()
                fileType = node.Attributes("type").Value.ToLower()
                '如果该文件已经添加过了就不用在添加了
                If (Not fileList.Contains(filePath)) Then
                    fileList.Add(filePath)
                    If fileType = "script" Then
                        output.Write(String.Format("<script type='text/javascript' src='{0}'></script>", filePath))
                    Else
                        output.Write(String.Format("<link rel='Stylesheet' href='{0}' type='text/css' />", filePath))
                    End If
                End If
            Next node
        End Sub

    End Class
End Namespace